package com.easygoods.controller;

import com.bestvike.stone.spring.controller.BaseController;
import com.bestvike.stone.spring.page.PageableRequest;
import com.bestvike.stone.spring.page.PageableResponse;
import com.bestvike.stone.spring.rest.common.CommonResponse;
import com.bestvike.stone.spring.shiro.annotation.RequiresPermissions;
import com.easygoods.request.AcpMemberRequest;
import com.easygoods.request.ChangeMobileBySmsRequest;
import com.easygoods.request.CouponListRequest;
import com.easygoods.response.AcpMemberResponse;
import com.easygoods.response.CouponRecordResponse;
import com.easygoods.response.CouponResponse;
import com.easygoods.response.GradeRightResponse;
import com.easygoods.response.MemberCashResponse;
import com.easygoods.response.MemberInfoResponse;
import com.easygoods.response.MissionResponse;
import com.easygoods.response.PointRecordResponseItem;
import com.easygoods.service.CouponService;
import com.easygoods.service.MemberService;
import com.easygoods.service.MissionService;
import com.easygoods.service.SmsService;
import com.easygoods.shiro.auth.UserPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/member")
public class MemberController extends BaseController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private MissionService missionService;
    @Autowired
    private SmsService smsService;

    //获取个人中心资料
    @RequiresPermissions(UserPermissions.BUY)
    @GetMapping("/getMemberInfo")
    public CommonResponse<MemberInfoResponse> getMemberInfo() {
        MemberInfoResponse response = this.memberService.getMemberCenter();
        return CommonResponse.success(response);
    }

    //查询该用户激励明细列表
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/getMemberCashList")
    public CommonResponse<PageableResponse<MemberCashResponse>> getMemberCashList(@RequestBody PageableRequest pageableRequest) {
        PageableResponse<MemberCashResponse> pageableResponse = this.memberService.getMemberCashList(pageableRequest);
        return CommonResponse.success(pageableResponse);
    }

    //查询该会员邀请的会员
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/getAcpMemberList")
    public CommonResponse<PageableResponse<AcpMemberResponse>> getAcpMemberList(@RequestBody AcpMemberRequest acpMemberRequest) {
        PageableResponse<AcpMemberResponse> pageableResponse = this.memberService.getAcpMemberList(acpMemberRequest);
        return CommonResponse.success(pageableResponse);
    }

    //查询该用户积分记录
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/getPointList")
    public CommonResponse<PageableResponse<PointRecordResponseItem>> getPointList(@RequestBody PageableRequest pageableRequest) {
        PageableResponse<PointRecordResponseItem> pageableResponse = this.memberService.getPointList(pageableRequest);
        return CommonResponse.success(pageableResponse);
    }

    //根据代金券状态查询该用户的代金券
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/getCouponList")
    public CommonResponse<CouponResponse> getCouponList(@RequestBody CouponListRequest couponListRequest) {
        CouponResponse couponResponse = this.couponService.getCouponList(couponListRequest);
        return CommonResponse.success(couponResponse);
    }

    //查询用户获得的代金券流水
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/getCouponRecordList")
    public CommonResponse<PageableResponse<CouponRecordResponse>> getCouponRecordList(@RequestBody PageableRequest pageableRequest) {
        PageableResponse<CouponRecordResponse> pageableResponse = this.couponService.getCouponRecordList(pageableRequest);
        return CommonResponse.success(pageableResponse);
    }

    //查询任务团队信息,我的团队
    @RequiresPermissions(UserPermissions.BUY)
    @GetMapping("/getCurrentMission")
    public CommonResponse<MissionResponse> getCurrentMission() {
        final MissionResponse mission = this.missionService.getMission();
        return CommonResponse.success(mission);
    }

    //获取等级权益
    @RequiresPermissions(UserPermissions.BUY)
    @GetMapping("/getGradeRightList")
    public CommonResponse<List<GradeRightResponse>> getGradeRightList(@RequestParam int grade) {
        final List<GradeRightResponse> gradeRightList = this.memberService.getGradeRightList(grade);
        return CommonResponse.success(gradeRightList);
    }

    //发送修改手机号验证码
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/sendChangeMobileSms")
    public CommonResponse<Object> sendChangeMobileSms() {
        this.smsService.sendChangeMobileSms();
        return CommonResponse.success();
    }

    //修改手机号 by sms
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/changeMobileBySms")
    public CommonResponse<Object> changeMobileBySms(@RequestBody ChangeMobileBySmsRequest request) {
        this.memberService.changeMobileBySms(request);
        return CommonResponse.success();
    }
}
